// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.UnifyResult;
using System.ComponentModel.DataAnnotations;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

/// <summary>
///     开发
/// </summary>
//[ValidateModelFilter]
[MaMvc("HtAdmin", controllerDescription: "开发", PCode = "HtAdmin_Developer")]
[AllowAnonymous]
public class DeveloperController : HtAdminBaseController
{
    private readonly ISysCacheService _cacheService;
    private readonly IDataConfigService _dataConfigService;
    private readonly IDeveloperService _developerService;
    private readonly ILogger<DeveloperController> _logger;

    public DeveloperController(ILogger<DeveloperController> logger
        , IDeveloperService developerService
        , ISysCacheService cacheService,
        IDataConfigService dataConfigService)
    {
        _logger = logger;
        _developerService = developerService;
        _cacheService = cacheService;
        _dataConfigService = dataConfigService;
    }

    [MaMenu("开发设置", Code = "Index")]
    [MaMenu("开发设置【演示】", Code = "Index2", UrlPar = "?u=1&a=2,u=2&a=2")]
    [MaPermission("浏览", "Index")]
    public IActionResult Index()
    {
        return View();
    }

    [MaPermission("浏览", "Index")]
    public IActionResult Index2(string name)
    {
        return View("Index");
    }

    /// <summary>
    ///     更新权限数据
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [MaPermission("更新权限数据", "UpdateMenuData")]
    [ValidateAntiForgeryToken]
    [AutoValidateAntiforgeryToken]
    public async Task<JsonResult> UpdateMenuData()
    {
        await _developerService.UpdateMenuPermission();
        return Json("");
    }

    /// <summary>
    ///     刷新缓存
    /// </summary>
    /// <returns></returns>
    [MaPermission("刷新缓存", "RefreshCache")]
    public async Task<JsonResult> RefreshCache()
    {
        await _cacheService.RefreshCache();
        return Json("");
    }

    [HttpGet]
    public async Task<ActionResult> UpdateMenu()
    {
        await _developerService.UpdateMenuPermission();
        return View();
    }

    [MaPermission("删除数据库所有菜单", "DeleteDbAllMenu")]
    [HttpPost]
    public async Task<JsonResult> DeleteDbAllMenu()
    {
        await _developerService.DeleteDbAllMenu();
        return Json("");
    }

    [HttpGet]
    public ActionResult TestApiPermission()
    {
        var m2 = AuthorityHelper.GetApiPermissions();
        return View();
    }

    [HttpGet]
    public ActionResult TestMvcPermission()
    {
        var m2 = AuthorityHelper.GetMenuPermissions();
        return View();
    }

    public IActionResult TestValidate()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TestValidate(TestValidateDto input)
    {
        return Json(new RESTfulResult<string> { Succeeded = true, Data = input.Name });
    }

    public class TestValidateDto
    {
        [Required(ErrorMessage = "名字必填")]
        [MinLength(5, ErrorMessage = "名字最小长度5")]
        public string Name { get; set; }

        [Required(ErrorMessage = "标题必填")]
        [MinLength(5, ErrorMessage = "标题最小长度5")]
        public string Title { get; set; }
    }

    #region RSA

    [MaMenu("RSA", Code = "Rsa")]
    [HttpGet]
    public IActionResult Rsa()
    {
        return View();
    }

    [HttpPost]
    public JsonResult RsaVer(string publicKey, string privateKey)
    {
        var result = _developerService.RsaVer(publicKey, privateKey);
        return Json(result);
    }

    #endregion RSA

    #region 生成代码

    /// <summary>
    ///     生成代码
    /// </summary>
    /// <returns></returns>
    [MaMenu("生成代码", Code = "GenerateCode")]
    [MaPermission("生成代码", "GenerateCode,GenerateCodeEdit,SubmitGenerateCode")]
    public async Task<IActionResult> GenerateCode()
    {
        var model = await _dataConfigService.GetSetting<CG_CodeGenerateConfig>(DataConfigDtoType.CodeGenerateConfig);
        return View(model);
    }

    /// <summary>
    ///     编辑保存代码生成配置
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<JsonResult> GenerateCodeEdit(CG_CodeGenerateConfig config)
    {
        await _dataConfigService.AddOrUpdateSetting(config, DataConfigDtoType.CodeGenerateConfig);
        return Json("");
    }

    /// <summary>
    ///     提交代码生成
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    [HttpPost]
    public JsonResult SubmitGenerateCode(CG_CodeGenerateConfig config)
    {
        CodeGenerateHelper.GenerateCode(config);
        return Json("");
    }

    #endregion

    #region 数据库差异对比

    /// <summary>
    ///     数据库差异对比
    /// </summary>
    /// <returns></returns>
    [MaMenu("数据库差异对比", Code = "DbDifferenceTables")]
    [MaPermission("数据库差异对比", "DbDifferenceTables")]
    [HttpGet]
    public async Task<IActionResult> DbDifferenceTables()
    {
        var model = await _dataConfigService.GetSetting<DbDifferenceTableConfig>(DataConfigDtoType
            .DbDifferenceTableConfig);
        return View(model);
    }

    /// <summary>
    ///     编辑保存数据库差异对比配置
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<JsonResult> DbDifferenceTables(DbDifferenceTableConfig config)
    {
        await _dataConfigService.AddOrUpdateSetting(config, DataConfigDtoType.DbDifferenceTableConfig);

        var result = _developerService.DbDifferenceTables(config);
        return Json(result);
    }

    #endregion
}